package com.api.gbhg.huangwm.action;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.RevenueBudgetDao;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author	Huangwm
 * @Date 2021-12-05
 * @Version 1.0
 * @description	TODO 预算编制流程节点附件操作扣减/释放收入预算--用于归档时先校验核批数据是否有变化 有则先释放原有金额再扣减 无则跳过 针对2023年调整编制核批后改动 2022年11月9日
 */
public class RevenueBudgetDeductionEndV2 implements Action {
    private static RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/RevenueBudgetDeductionEnd",false);

    @Override
    public String execute(RequestInfo requestInfo) {
        logTool.writeLog("RevenueBudgetDeductionEnd预算编制归档时校验核批数据是否有变化 requestid="+requestInfo.getRequestid());

        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id

        List<Map<String,String>> getSNDatas1 = new ArrayList<>();//上年结转 当前数据
        List<Map<String,String>> getDNDatas1 = new ArrayList<>();//当年预算 当前数据

        List<Map<String,String>> getSNDatas2 = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas2 = new ArrayList<>();//当年预算

        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("获取预算年度为空");
                return  FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取预算年度异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        try {
            getSNDatas1 = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转 当前数据
            getDNDatas1 = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算 当前数据
            logTool.writeLog(" 上年结转1 "+getSNDatas1);
            logTool.writeLog(" 当年预算1 "+getDNDatas1);

            getSNDatas2 = budgetUtils.getSNDatas_02(requestInfo.getRequestid());//上年结转
            getDNDatas2 = budgetUtils.getDNDatas_02(requestInfo.getRequestid());//当年预算
            logTool.writeLog(" 上年结转2 "+getSNDatas2);
            logTool.writeLog(" 当年预算2 "+getDNDatas2);
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取上年结转和当年预算核批数据异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }

        //判断数据
        boolean snjz = JSONObject.toJSONString(getSNDatas1).equals(JSONObject.toJSONString(getSNDatas2));
        boolean dnys = JSONObject.toJSONString(getDNDatas1).equals(JSONObject.toJSONString(getDNDatas2));
        if(!snjz || !dnys){
            logTool.writeLog("核批数据有变化  先按原核批内容释放 snjz="+snjz+" dnys="+dnys);
            Map<String, Object> result = budgetUtils.RevenueBudgetReturnV2(getSNDatas2,getDNDatas2,dw,ysnd);
            logTool.writeLog("释放结果："+JSONObject.toJSONString(result));
            if(!result.get("code").equals("1")){
                String f1 = result.get("f1").toString();
                String f2 = result.get("f2").toString();
                String f3 = result.get("f3").toString();
                String f4 = result.get("f4").toString();
                String f5 = result.get("f5").toString();
                String f6 = result.get("f6").toString();
                String f7 = result.get("f7").toString();
                String f8 = result.get("f8").toString();
                String msg = "<br />"+f1+f2+f3+f4+f5+f6+f7+f8;
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("核批数据有变化  先按原核批内容释放金额失败 "+msg);
                return  FAILURE_AND_CONTINUE;
            }else{
                //释放成功  按照当前的扣减
                Map<String, Object> result2 = budgetUtils.RevenueBudgetDeduction(getSNDatas1,getDNDatas1,dw,ysnd);
                logTool.writeLog("重新扣减结果："+JSONObject.toJSONString(result2));
                if(!result2.get("code").equals("1")){
                    String f1 = result.get("f1").toString();
                    String f2 = result.get("f2").toString();
                    String f3 = result.get("f3").toString();
                    String f4 = result.get("f4").toString();
                    String f5 = result.get("f5").toString();
                    String f6 = result.get("f6").toString();
                    String f7 = result.get("f7").toString();
                    String f8 = result.get("f8").toString();
                    String msg = "<br />"+f1+f2+f3+f4+f5+f6+f7+f8;
                    requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                    requestInfo.getRequestManager().setMessagecontent("释放后重新扣减金额失败 "+msg);
                    return  FAILURE_AND_CONTINUE;
                }else{
                    //再重新记录核批数据
                    String xmmc = budgetUtils.getFieldValue("xmmc", flowTableName, requestInfo.getRequestid() + "");
                    String sqze = budgetUtils.getFieldValue("sqzey", flowTableName, requestInfo.getRequestid() + "");
                    String hpze = budgetUtils.getFieldValue("hpze", flowTableName, requestInfo.getRequestid() + "");
                    Map<String, String> mainData = new HashMap<>();
                    mainData.put("lcid", requestInfo.getRequestid() + "");
                    mainData.put("ysnd", ysnd);
                    mainData.put("xmmc", xmmc);
                    mainData.put("sqze", sqze);
                    mainData.put("hpze", hpze);
                    mainData.put("version", "end");
                    Map<String, Object> result3 = budgetUtils.setPeojectApprovalRecords(mainData, getSNDatas1, getDNDatas1);
                    if (!result3.get("code").equals("1")) {
                        boolean f1 = (boolean) result3.get("f1");
                        boolean f2 = (boolean) result3.get("f2");
                        boolean f3 = (boolean) result3.get("f3");
                        String msg = "";
                        if (!f1) {
                            msg += "插入主表数据SQL执行异常;";
                        }
                        if (!f2) {
                            msg += "插入上年结转核批数据SQL执行异常;";
                        }
                        if (!f3) {
                            msg += "插入当年预算核批数据SQL执行异常;";
                        }
                        logTool.writeLog("存档核批数据异常 msg=" + msg);
                        requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                        requestInfo.getRequestManager().setMessagecontent("存档核批数据异常>" + msg);
                        return FAILURE_AND_CONTINUE;
                    }
                }
            }
        }

        return SUCCESS;
    }
}
